package com.tuanzi.loan.business.repository.settlement.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_COMPANY_INFO;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_SETTLEMENT_PRICE_CHANGE_RECORD;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.settlement.custom.SettlementPriceChangeRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.settlement.SettlementPriceChangeRecordRequest;
import com.tuanzi.loan.vo.response.settlement.SettlementPriceChangeRecordResponse;

/**
 * @author caiwanlin
 * @create 2018-01-16 16:10
 */
public class SettlementPriceChangeRecordRepositoryImpl extends JOOQBaseRepository implements SettlementPriceChangeRecordRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_PRICE_CHANGE_RECORD;
    }

    @Override
    public Page<SettlementPriceChangeRecordResponse> list(SettlementPriceChangeRecordRequest searchRequest, Pageable pageable) {
        fields(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.ID,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.UPDATOR,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.UPDATE_TIME,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.OLD_PERCENT_PRICE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_PERCENT_PRICE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.OLD_YUAN_PRICE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_YUAN_PRICE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.EFFECTIVE_TIME,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COMPANY_NAME,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.PRODUCT_NAME,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COP_MODE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_COP_MODE,
                LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COMPANY_ID);
//        leftJoin(LOAN_COMPANY_INFO,LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COMPANY_ID.eq(LOAN_COMPANY_INFO.ID));
        between(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.UPDATE_TIME,searchRequest.getStartTime(),searchRequest.getEndTime());
        contains(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COMPANY_NAME,searchRequest.getCompanyName());
//        contains(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.PRODUCT_NAME,searchRequest.getProductName());
        return search(pageable,new SettlementPriceChangeRecordResponseRecordMapper());
    }

    private final class SettlementPriceChangeRecordResponseRecordMapper implements RecordMapper<Record, SettlementPriceChangeRecordResponse>{

        @Override
        public SettlementPriceChangeRecordResponse map(Record record) {
            SettlementPriceChangeRecordResponse response = new SettlementPriceChangeRecordResponse();
            response.setId(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.ID));
            response.setCreator(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.UPDATOR));
            response.setCreateTime(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.UPDATE_TIME));
            response.setCompanyName(record.get(LOAN_COMPANY_INFO.COMPANY_NAME));
            response.setProductName(record.get(LOAN_COMPANY_INFO.PRODUCT_NAME));
            response.setOldPercentPrice(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.OLD_PERCENT_PRICE,Float.class));
            response.setNewPercentPrice(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_PERCENT_PRICE,Float.class));
            response.setOldYuanPrice(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.OLD_YUAN_PRICE,Float.class));
            response.setNewYuanPrice(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_YUAN_PRICE,Float.class));
            response.setCopMode(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COP_MODE));
            response.setNewCopMode(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.NEW_COP_MODE));
            response.setCompanyId(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.COMPANY_ID));
            response.setEffectiveTime(record.get(LOAN_SETTLEMENT_PRICE_CHANGE_RECORD.EFFECTIVE_TIME));
            return response;
        }
    }


}
